﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using V2TEK.NET.SH.Silverlight.Charts.Core.Schedule;

namespace V2TEK.NET.SH.Silverlight.Charts.GanttHelper
{
    /// <summary>
    /// 根据任务类型决定使用何种Renderer展示任务
    /// Determine which renderer will be used according to TaskType.(SummaryTaskRenderer，TaskRenderer)
    /// </summary>
    public class SimpleTaskEntryRenderer : AbstractTaskEntryRenderer
    {
        AbstractTaskEntryRenderer _renderer;

        public SimpleTaskEntryRenderer() : base()
        {
            this.DefaultStyleKey = typeof(SimpleTaskEntryRenderer);
        }

        public StackPanel RenderRoot
        {
            get;
            set;
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            this.RenderRoot = this.GetTemplateChild("RenderRoot") as StackPanel;// this.Children.Add(this.Element);
            this.RenderRoot.Children.Add(this.Renderer);

            Binding bdata = new Binding("Data");
            bdata.Source = this;
            this.Renderer.SetBinding(AbstractTaskEntryRenderer.DataProperty, bdata);

            Binding bs = new Binding("Selected");
            bs.Source = this;
            this.Renderer.SetBinding(AbstractTaskEntryRenderer.SelectedProperty, bs);

        }

        internal AbstractTaskEntryRenderer Renderer
        {
            get
            {
                if (_renderer == null)
                {
                    switch (this.Data.TaskType)
                    {
                        case TaskType.ControlAccount:
                        case TaskType.ControlAccountPlan:
                        case TaskType.PlanningPackage:
                        case TaskType.SummaryTask:
                        case TaskType.WorkPackage:
                            _renderer = new SimpleSummaryTaskRenderer();
                            break;
                        case TaskType.CriticalPath:
                        case TaskType.Task:
                        case TaskType.Milestone:
                        default:
                            _renderer = new SimpleTaskRenderer();
                            break;
                    }
                }
                return _renderer;
            }
        }

        protected override Size ArrangeOverride(Size finalSize)
        {
            this.Renderer.Arrange(new Rect(new Point(0, 0), finalSize));
            return finalSize;
        }

        protected override Size MeasureOverride(Size availableSize)
        {
            this.Renderer.Measure(availableSize);
            return availableSize;
        }
    }
}
